package com.jinkeen.xiaxie.banner

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.jinkeen.xiaxie.R

/**
 * @description: TODO
 * @author whz
 * @date 2023-07-01 11:03
 * @version 1.0
 */
class ViewPagerAdapter(private var colors: List<Int>) :
    RecyclerView.Adapter<ViewPagerAdapter.ViewHolder>() {

    //    加载布局视图
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_banner, parent, false))

    //绑定数据
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val i = position % 5//有多少个页面就写多少,这里有物种颜色就写5，除以5取余数
        holder.tvTitle?.text = "第$i 个页面"
        holder.container?.setBackgroundColor(colors[i])
    }

    override fun getItemCount(): Int = Int.MAX_VALUE

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var container: LinearLayout? = null
        var tvTitle: TextView? = null

        init {
            container = itemView.findViewById(R.id.container)
            tvTitle = itemView.findViewById(R.id.tv_title)
        }
    }
}